package com.example.demo.controller;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.example.demo.entity.ApiResponse;
import com.example.demo.service.RedisService;
import com.example.demo.utrl.SendSMSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
    public class SendSMSControllter {
        @Autowired
        private RedisService redisservice;
        @RequestMapping("/sendSMS")
        public ResponseEntity<ApiResponse> sendSMS(@RequestParam String phoneNumber) {
            String code = "666666";
            try {
                SendSmsResponse response = SendSMSUtil.sendSms(phoneNumber, code);
                if ("OK".equalsIgnoreCase(response.getCode())) {
                    redisservice.setValue("code",code);
                    System.out.println(redisservice.getValue("code"));
                    return ResponseEntity.ok(new ApiResponse(200, "发送成功", null));
                } else {
                    return ResponseEntity.badRequest().body(new ApiResponse(400, "发送失败: " + response.getMessage(), null));
                }
            } catch (ClientException e) {
                e.printStackTrace();
                return ResponseEntity.internalServerError().body(new ApiResponse(500, "服务器错误: " + e.getMessage(), null));
            }
        }
    }

